* ==========================================================
* ----------- CNES 1992
* ==========================================================

use "${dir_raw}/CNES_1.dta",clear 

gen id = CASEID 
gen year = 1992 

gen state = STNO 

gen r_female = V87==5 if V87 < 8 
gen r_educ = V173 if V173 < 98

recode V182 (1=1) (2=2) (3/5=4) (8/9=.), gen(r_race)
replace r_race = 3 if V183 == 1
	gen r_white = r_race == 1 if ~missing(r_race)
	gen r_black = r_race == 2 if ~missing(r_race)
	gen r_others = r_race == 3 | r_race == 4 if ~missing(r_race)


gen r_borny = V181 if V181 < 2000
gen r_age = year - r_borny

gen r_marstat = V137 if V137 < 8
	gen r_married = r_marstat == 1 if ~missing(r_marstat)
	gen r_single = r_marstat == 5 if ~missing(r_marstat)

gen r_child = V138 ==1 if V138 < 8
gen r_wrkstat = V139 if V139 < 8 
	recode r_wrkstat (1=1) (else=0), gen(r_working)
	recode r_wrkstat (2=1) (else=0), gen(r_unemployed)
	recode r_wrkstat (3=1) (else=0), gen(r_retired)
	recode r_wrkstat (4 5 0=1) (else=0), gen(r_otherworks)
*<1> employed [goto z10]
*<2> unemployed
*<3> retired
*<4> keeping house
*<5> student
*<0> other (specify) [specify]
*<8> DK

gen r_pol_interest = 5-V51 if V51 < 8

*V51 >c1< Now I have some general questions about public
*affairs and election campaigns.
*Some people don't pay much attention to political
*campaigns. During this recent election, were you very
*much interested, somewhat interested, or not much
*interested in the political campaigns.
*<1> very much interested
*<3> somewhat interested
*<5> not much interested
*<8> DK
*<9> RF
*===>

gen r_no_religion = V141 == 5 if V141 < 8 
gen r_attendance = 5 - V143 if V143 < 8 
*<1> every week
*<2> almost every week
*<3> once or twice a month
*<4> a few times a year, or
*<5> never [goto e1a]

gen r_ideo = V79 if V79 < 96 
gen r_ideo7 = (r_ideo-1) / 9 * 6 + 1
*Using any number between 1 and 10, where do you place
*yourself on this scale when you think of your own
*political views?
*<1> most liberal
*<2-9>
*<10> most conservative
*(VOLUNTEERED)(PROBE)
*<96> don't think along those lines
*<97> depends

tab V53 V52
gen r_partyid7 = . 
replace r_partyid7 = 1 if V52 == 1 & (V54 == 1 | V54 == 2)
replace r_partyid7 = 2 if V52 == 1 & (V54 == 3 | V54 == 4 | V54 == 5)
replace r_partyid7 = 3 if (V52 == 3 | V52 == 4 | V52 == 5) & V53 == 5
replace r_partyid7 = 4 if (V52 == 3 | V52 == 4 | V52 == 5) & (V53 == 7|V53 == 8) 
replace r_partyid7 = 5 if (V52 == 3 | V52 == 4 | V52 == 5) & V53 == 1
replace r_partyid7 = 6 if V52 == 2 & (V54 == 3 | V54 == 4 | V54 == 5)
replace r_partyid7 = 7 if V52 == 2 & (V54 == 1 | V54 == 2)

gen r_party_int = abs(r_partyid7-4)
gen r_ideo_int = abs(r_ideo7-4)

* state indicators
bysort state: egen state_n = count(id)
encode state, gen(state_no)

* network information 
gen a1_network = V185
gen a1_name = V186

gen a2_network = V187
gen a2_name = V188

gen a3_network = V189
gen a3_name = V190

gen a4_network = V191
gen a4_name = V192

gen a5_network = V193  // political 
gen a5_name = V194 

egen n_size_imp = anycount(a1_network a2_network a3_network a4_network),values(1)
egen n_size_pol = anycount(a1_network a2_network a3_network a4_network a5_network),values(1)

gen n_size_addpol = a5_network == 1 if ~missing(a5_network)

* also network type / gender / years of education /cowork-neighbor/ church /closeness
gen a1_type = V195 				if V195 < 8
*<1> spouse or partner [goto h3dz]
*<2> other relative (specify), or [specify]
*<3> unrelated by blood or marriage [goto tmt1]
gen a1_cowork = V196 == 1 	if V196 < 8 
gen a1_same_church = V197 == 1 		if V197 < 8 
gen a1_neighbor = V198 == 1 	if V198 < 8 
gen a1_friend = V199 == 1 		if V199 < 8 
gen a1_closefriend = V200 == 1 	if V200 < 8 
gen a1_educ = V201 				if V201 < 8 
*<1> less than high school
*<3> a high school diploma, or
*<5> a college degree
gen a1_talkpol = 4 - V202 		if V202 < 5 
*<1> often
*<2> sometimes
*<3> rarely, or
*<4> never[goto h3jz]
gen a1_disagree = 4 - V203 		if V203 < 5 
gen a1_female = V204 == 5 		if V204 < 8
gen a1_knowpolitics = 5 - V205 	if V205 <= 5

gen a1_vote = V206 				
*<1> Bush
*<2> Clinton
*<3> Perot
*<4> other (specify)[specify]
*<5> Bush & Clinton
*<6> Bush & Perot
*<7> Clinton & Perot
*<0> none
*<8> DK
*<9> RF

gen a2_type = V207 				if V207 < 8
*<1> spouse or partner [goto h3dz]
*<2> other relative (specify), or [specify]
*<3> unrelated by blood or marriage [goto tmt1]
gen a2_cowork = V208 == 1  	if V208 < 8 
gen a2_same_church = V209 == 1    	if V209 < 8 
gen a2_neighbor = V210 == 1  	if V210 < 8 
gen a2_friend = V211 == 1    	if V211 < 8 
gen a2_closefriend = V212 == 1  if V212 < 8 
gen a2_educ = V213 				if V213 < 8 
gen a2_talkpol = 4 - V214 		if V214 < 5 
gen a2_disagree = 4 - V215 		if V215 < 5 
gen a2_female = V216 == 5 		if V216 < 8
gen a2_knowpolitics = 5 - V217 	if V217 <= 5
gen a2_vote = V218 				

gen a3_type = V219 				if V219 < 8
gen a3_cowork = V220 == 1  	if V220 < 8 
gen a3_same_church = V221 == 1    	if V221 < 8 
gen a3_neighbor = V222 == 1  	if V222 < 8 
gen a3_friend = V223 == 1    	if V223 < 8 
gen a3_closefriend = V224 == 1  if V224 < 8 
gen a3_educ = V225 				if V225 < 8 
gen a3_talkpol = 4 - V226 		if V226 < 5 
gen a3_disagree = 4 - V227 		if V227 < 5 
gen a3_female = V228 == 5 		if V228 < 8
gen a3_knowpolitics = 5 - V229 	if V229 <= 5
gen a3_vote = V230 				

gen a4_type = V231 				if V231 < 8
gen a4_cowork = V232 == 1  	if V232 < 8 
gen a4_same_church = V233 == 1    	if V233 < 8 
gen a4_neighbor = V234 == 1  	if V234 < 8 
gen a4_friend = V235 == 1    	if V235 < 8 
gen a4_closefriend = V236 == 1  if V236 < 8 
gen a4_educ = V237 				if V237 < 8 
gen a4_talkpol = 4 - V238 		if V238 < 5 
gen a4_disagree = 4 - V239 		if V239 < 5 
gen a4_female = V240 == 5 		if V240 < 8
gen a4_knowpolitics = 5 - V241 	if V241 <= 5
gen a4_vote = V242 				

gen a5_type = V243 				if V243 < 8
gen a5_cowork = V244 == 1  	if V244 < 8 
gen a5_same_church = V245 == 1    	if V245 < 8 
gen a5_neighbor = V246 == 1  	if V246 < 8 
gen a5_friend = V247 == 1    	if V247 < 8 
gen a5_closefriend = V248 == 1  if V248 < 8 
gen a5_educ = V249 				if V249 < 8 
gen a5_talkpol = 4 - V250 		if V250 < 5 
gen a5_disagree = 4 - V251 		if V251 < 5 
gen a5_female = V252 == 5 		if V252 < 8
gen a5_knowpolitics = 5 - V253 	if V253 <= 5
gen a5_vote = V254 				


* delete all non-political ties 
forvalues i = 1/4 {
	replace a`i'_network = 5 if a`i'_talkpol == 0 & ~missing(a`i'_talkpol)
	replace a`i'_type = . if a`i'_talkpol == 0 & ~missing(a`i'_talkpol)
	replace a`i'_cowork = . if a`i'_talkpol == 0 & ~missing(a`i'_talkpol)
	replace a`i'_same_church = . if a`i'_talkpol == 0 & ~missing(a`i'_talkpol)
	replace a`i'_neighbor = . if a`i'_talkpol == 0 & ~missing(a`i'_talkpol)
	replace a`i'_friend = . if a`i'_talkpol == 0 & ~missing(a`i'_talkpol)
	replace a`i'_closefriend = . if a`i'_talkpol == 0 & ~missing(a`i'_talkpol)
	replace a`i'_educ = . if a`i'_talkpol == 0 & ~missing(a`i'_talkpol)
	replace a`i'_disagree = . if a`i'_talkpol == 0 & ~missing(a`i'_talkpol)
	replace a`i'_female = . if a`i'_talkpol == 0 & ~missing(a`i'_talkpol)
	replace a`i'_knowpolitics = . if a`i'_talkpol == 0 & ~missing(a`i'_talkpol)
	replace a`i'_vote = . if a`i'_talkpol == 0 & ~missing(a`i'_talkpol)
}

* network size 
egen n_size = anycount(a1_network a2_network a3_network a4_network a5_network),values(1)

forvalues i = 1/5{
	gen a`i'_relative = a`i'_type 	
	recode a`i'_relative (1 2 =1) (3=0)
}

tab a5_relative
forvalues i = 1/5{
	gen a`i'_male = 1- a`i'_female
}


*** vote uncertainty
*<1> Bush
*<2> Clinton
*<3> Perot
*<4> other (specify)[specify]
*<5> Bush & Clinton
*<6> Bush & Perot
*<7> Clinton & Perot
*<0> none
*<8> DK
*<9> RF

gen a1_vote_dk = a1_vote >7 if ~missing(a1_vote)
gen a2_vote_dk = a2_vote >7 if ~missing(a2_vote)
gen a3_vote_dk = a3_vote >7 if ~missing(a3_vote)
gen a4_vote_dk = a4_vote >7 if ~missing(a4_vote)
gen a5_vote_dk = a5_vote >7 if ~missing(a5_vote)

egen p_vote_dk = rowmean(a?_vote_dk)
egen n_vote_dk = rowtotal(a?_vote_dk)
replace n_vote_dk = . if missing(n_size_pol)
replace n_vote_dk = . if n_size_pol == 0

* with partner -- 
gen a0_talkpol = 4-V255 if V255 < 8

gen a0_disagree = 4-V256 if V256 < 8 & a0_talkpol > 0 
gen a0_knowpolitics = 5-V257 if V257 <= 5 & a0_talkpol > 0 
gen a0_vote = V258 if V258 < 8 & a0_talkpol > 0 

gen a0_vote_dk = V258 > 7 if ~missing(V258) & a0_talkpol > 0 


forvalues i = 0/5{
	*recode a`i'_vote (0=.) (1=3) (2=1) (3/7=2) (8/9=.)
	recode a`i'_vote (0=-1) (1=1) (2=0) (3/7=0.5) (8/9=.), gen(a`i'_bush)
	recode a`i'_vote (0=0) (1/7=1) (8/9=.)
}

gen r_vote = V43
recode r_vote (3/7=0) (8/9=.)

gen r_bush = V44 
recode r_bush (0=-1) (1=1) (3=0) (5/7=0.5) (8/9=.)
replace r_bush = -1 if r_vote == 0 

gen a0_vote_same = (r_bush == a0_bush) if ~missing(r_bush) & ~missing(a0_bush)
gen a1_vote_same = (r_bush == a1_bush) if ~missing(r_bush) & ~missing(a1_bush)
gen a2_vote_same = (r_bush == a2_bush) if ~missing(r_bush) & ~missing(a2_bush)
gen a3_vote_same = (r_bush == a3_bush) if ~missing(r_bush) & ~missing(a3_bush)
gen a4_vote_same = (r_bush == a4_bush) if ~missing(r_bush) & ~missing(a4_bush)
gen a5_vote_same = (r_bush == a5_bush) if ~missing(r_bush) & ~missing(a5_bush)

* below is to exclude "non-voters"
gen r_bush_a = r_bush if r_bush >= 0 & ~missing(r_bush)

gen a0_vote_bush_a = a0_bush if a0_bush >= 0 & ~missing(a0_bush)
gen a1_vote_bush_a = a1_bush if a1_bush >= 0 & ~missing(a1_bush)
gen a2_vote_bush_a = a2_bush if a2_bush >= 0 & ~missing(a2_bush)
gen a3_vote_bush_a = a3_bush if a3_bush >= 0 & ~missing(a3_bush)
gen a4_vote_bush_a = a4_bush if a4_bush >= 0 & ~missing(a4_bush)
gen a5_vote_bush_a = a5_bush if a5_bush >= 0 & ~missing(a5_bush)

forvalues i = 0/5{
	gen a`i'_vote_same_a = (a`i'_vote_bush_a == r_bush_a) if ~missing(a`i'_vote_bush_a) & ~missing(r_bush_a)
}

egen n_same_vote = rowtotal(a?_vote_same)
replace n_same_vote = . if missing(n_size_pol)

egen p_same_vote = rowmean(a?_vote_same)

* zip-code 
gen zipcode =V268

gen dataset = "CNES"

* weight = equal weights
gen wtall = 1 

gen i_set = SETA 
tab SETA 

* create dyad data files
preserve 

keep id a?_*

reshape long a@_network a@_name a@_type a@_cowork a@_neighbor a@_friend a@_closefriend a@_relative /*
*/ a@_educ a@_male a@_same_church a@_vote_same a@_vote_dk a@_bush /*
*/ a@_knowpolitics a@_vote a@_talkpol a@_disagree, i(id) j(order)

drop if a_network == 5 | missing(a_network)
drop if a_talkpol == 0 
saveold "${dir_processed}/dyad_1992.dta", replace version(12)
restore 

* create individual data files
keep id wtall zipcode state* r_* a* dataset year n_* i_*  p_*

saveold "${dir_processed}/cnes_1992.dta", replace version(12)
